package com.scanner.net;


import com.scanner.util.LogUtil;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by 皓然 on 2017/7/5.
 */

public class MainRetrofit {
    private EDAppService comicService;

    private static final int DEFAULT_TIMEOUT = 15;

    private Retrofit retrofit;
    private RxFileDownLoadObserver<File> fileDownLoadObserver;

    //  .addInterceptor(new HttpCommonInterceptor())
    MainRetrofit() {
        retrofit = new Retrofit.Builder()
                .client(getClient(null))
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(InterBase.HTTPURL)
                .build();

        comicService = retrofit.create(EDAppService.class);
    }

    public EDAppService getService(){
        return comicService;
    }


    private OkHttpClient getClient(RxDownLoadInterceptor mInterceptor){
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new HttpCommonInterceptor())
                .addNetworkInterceptor(mInterceptor == null ? new Interceptor() {
                    @Override
                    public okhttp3.Response intercept(Chain chain) throws IOException {
                        okhttp3.Response orginalResponse = chain.proceed(chain.request());
                        return orginalResponse.newBuilder()
                                .body(new ProgressResponseBody(orginalResponse.body(), new ProgressListener() {
                                    @Override
                                    public void onProgress(long progress, long total, boolean done) {
                                        LogUtil.d("body响应速度"+progress+";文件大小"+total);
                                    }
                                }))
                                .build();
                    }
                } : mInterceptor)
                .sslSocketFactory(TrustAllCerts.createSSLSocketFactory())
                .hostnameVerifier(new TrustAllCerts.TrustAllHostnameVerifier())
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();
        return client;
    }


    public void downloadSigFile(String downUrl,final String filePath, final String fileName, RxProgressListener progressListener){

        fileDownLoadObserver = new RxFileDownLoadObserver<>(progressListener);
        RxDownLoadInterceptor mInterceptor = new RxDownLoadInterceptor(progressListener);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(InterBase.HTTPURL)
                .client(getClient(mInterceptor))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        progressListener.onStartDownload();

        retrofit.create(EDAppService.class)
                .download(downUrl)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .map(new Function<ResponseBody, File>() {
                    @Override
                    public File apply(ResponseBody responseBody) throws Exception {
                        return fileDownLoadObserver.saveFile(responseBody, filePath, fileName);
                    }
                })
                .observeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(fileDownLoadObserver);

    }

}
